import {Component, ElementRef, HostBinding, OnInit, Renderer2, ViewEncapsulation} from '@angular/core';
import {AppService} from "../../../app.service";
import {GlobalEvent} from "../../../support/event.definition";

@Component({
  selector: 'ss-platform-loading',
  templateUrl: './loading.component.html',
  host: {class: "ss-platform-loading-wraper"},
  encapsulation: ViewEncapsulation.None
})
export class LoadingComponent implements OnInit {

  @HostBinding("class.in")
  isVisibility: boolean = true;
  @HostBinding("class.complete")
  isComplete: boolean = false;

  constructor(private element: ElementRef, private renderer: Renderer2, private $app: AppService) {
    let loadingElement = element.nativeElement;
    let parentElement = loadingElement.parentElement;
    this.$app.subscribe(GlobalEvent.ShowLoading, (eventName, data, resolve) => {
      loadingElement.classList.remove("hide");
      // parentElement.append(loadingElement);
      if (!loadingElement.classList.contains("complete")) {
        loadingElement.classList.add("complete")
      }
      this.isVisibility = true;
      setTimeout(() => resolve(true), 200);
    });
    this.$app.subscribe(GlobalEvent.HideLoading, (eventName, data, resolve) => {
      this.isVisibility = false;
      setTimeout(() => {
        loadingElement.classList.add("hide");
        // parentElement.contains(loadingElement) && parentElement.removeChild(loadingElement);
        resolve(true);
      }, 200);
    });

    // setTimeout(() => {
    //   this.$app.publish(GlobalEvent.ShowLoading).then(() => {
    //     console.log("show loading")
    //     /*setTimeout(() => {
    //       this.$app.publish(GlobalEvent.HideLoading).then(() => {
    //         console.log("hide loading")
    //       });
    //     }, 2000)*/
    //   });
    // }, 2000)
  }

  ngOnInit(): void {
  }

}
